<?php
/**
 * JS_API支付demo
 * ====================================================
 * 在微信浏览器里面打开H5网页中执行JS调起支付。接口输入输出数据格式为JSON。
 * 成功调起支付需要三个步骤：
 * 步骤1：网页授权获取用户openid
 * 步骤2：使用统一支付接口，获取prepay_id
 * 步骤3：使用jsapi调起支付
 * 
*/ 
   define('BASEPATH', str_replace("\\", "/", 'system'));
   include_once(getcwd()."/application/libraries/scenicwxpay/WxPayPubHelper/WxPayPubHelper.php"); //引入微信类文件
   include_once(getcwd()."/application/libraries/xcrypt.php"); //引入加密类
   
   $key = 'd77ab88a8400ed08'; //aes 密钥
   $iv = '6c031d737fba7fcf'; //aes 偏移量
   $clientKey = $_GET['clientKey']; //加密数据
   if (empty($clientKey)) {
       header('Location:http://wap.zxkj918.com/m/home/grid');
   }
   
   /*获取openid*/
   if(!isset($_GET['code']))
   {
       $return_url = urlencode("http://wap.zxkj918.com/hotel_wx_call.php?clientKey=".$clientKey);
       $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3c5437a8f56b384e&redirect_uri=".$return_url."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
       header("Location:$url");
   }else{
   
       $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3c5437a8f56b384e&secret=d54eef1638a1c094d33e15bf2d0b1f66&code=".$_GET['code']."&grant_type=authorization_code";
       $ch  = curl_init();
       curl_setopt($ch, CURLOPT_URL,$url);
       curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);   //返回原生的（Raw）输出
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //可以设置为验证站点
       $result = curl_exec($ch);
       curl_close($ch);//解析 得到openid,打开绑定页面 传openid
       $resource = json_decode($result);
       $openid = trim($resource->openid);
   }
   
   
   $xcrypt = new xcrypt(); //加密类的处理
   $xcrypt->init($key, 'cbc', $iv);
   $screteData = $xcrypt->decrypt($clientKey,'hex');
   $screteDataArray = explode('_',$screteData);
   
   $pay_id       =  $screteDataArray[0];
   $total_fee    =  $screteDataArray[1]*100;
   $notify_url   =  "http://wap.zxkj918.com/hotel/paycallback/wxNotify";

	//使用jsapi接口
   $jsApi        =  new JsApi_pub();	
   $unifiedOrder =  new UnifiedOrder_pub();
   $unifiedOrder->setParameter("openid","$openid");//商品描述
   $unifiedOrder->setParameter("body","酒店支付");//商品描述
   $timeStamp    =  time();
   $unifiedOrder->setParameter("out_trade_no","$pay_id");//商户订单号 
   $unifiedOrder->setParameter("total_fee","$total_fee");//总金额   以分为单位
   $unifiedOrder->setParameter("notify_url",$notify_url);//异步通知地址 
   $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
   $prepay_id    =  $unifiedOrder->getPrepayId();
   //使用jsapi调起支付
   $jsApi->setPrepayId($prepay_id);
   $jsApiParameters = $jsApi->getParameters();
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>微信安全支付</title>
<script type="text/javascript">
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',
			<?php echo $jsApiParameters; ?>,
			function(res){
				WeixinJSBridge.log(res.err_msg);
                window.location.href="http://wap.zxkj918.com/m/pay/wxSuccessCompletePay/"+<?php echo $pay_id;?>+"?err_msg="+res.err_msg;
			}
		);
	}
	
	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	window.onload=callpay;
</script>
</head>
<body>
</body>
</html>